﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;


namespace GUI.BaoCao.BCxuathuyhanghoa
{
    public partial class frmBCXuatHuyHangHoa : Form
    {

        #region khai báo
        bool kiemtra = false;
        bool CheckClose = false;
        DateTime date;
        DateTime batdau;
        DateTime ketthuc;
        Entities.KhoHang[] khohang;
        ArrayList array;
        ArrayList array1;
        string makho = "";
        string tenkho = "";
        Entities.BCXuatHuyHangHoa[] hienthi;
        Entities.BCXuatHuyHangHoa[] baocao;
        Entities.BCXuatHuyHangHoa save;
        Entities.ChiTietXuatHuy[] chitietxuathuy;
        Entities.PhieuXuatHuy[] phieuxuathuy;
        Entities.PhieuXuatHuy phieuxuat;
        Entities.KhoHang khohang1;
        System.Windows.Forms.SaveFileDialog savefile;
        int i = 0;
        #endregion

        #region hiển thị
        private Entities.BCXuatHuyHangHoa[] hienthibaocao;
        Entities.BCXuatHuyHangHoa[] danhsach;
        Entities.BCXuatHuyHangHoa xuathuy;
        Entities.KhoHang[] khoh;
        public frmBCXuatHuyHangHoa()
        {
            InitializeComponent();
            try
            {

                Entities.CaiDatKho kho = GUI.Entitystatic.ViewFile();
                makho = kho.MaKho;
                tenkho = kho.TenKho;
                if (makho.Length == 0 || makho == "NULL")
                {
                    MessageBox.Show(Common.MSG.MSG28(), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }
                xuathuy = Common.StaticVariable.InterfaceObject.BCXuatHuyHangHoa();
                 Common.Utilities.XuLyComboBox(comboBox1, khoh, Common.Constants.KhoHang.TenKho, Common.Constants.KhoHang.MaKho);
                 khohang = Common.StaticVariable.InterfaceObject.SelectKhoHang(khohang1);
                 date = Common.StaticVariable.InterfaceObject.SelectDate();
                 batdau = ketthuc = date;
                 this.HienThiListview();
                 comboBox1.Text = tenkho;
                savefile = new System.Windows.Forms.SaveFileDialog();
            }
            catch
            { }
        }
        private void HienThiListview()
        {
            try
            {
                if (hienthi == null)
                {
                    Entities.BCXuatHuyHangHoa[] nv = new Entities.BCXuatHuyHangHoa[0];
                    dgvHienThi.DataSource = nv;
                }
                for (int i = 0; i < dgvHienThi.ColumnCount; i++)
                {
                    dgvHienThi.Columns[i].Visible = false;
                }
                dgvHienThi.Columns["maKho"].HeaderText = "Mã Kho";
                dgvHienThi.Columns["maKho"].Visible = true;
                dgvHienThi.Columns["tenKho"].HeaderText = "Tên kho ";
                dgvHienThi.Columns["tenKho"].Visible = true;
                dgvHienThi.Columns["maChungTu"].HeaderText = "Mã chứng từ";
                dgvHienThi.Columns["maChungTu"].Visible = true;
                dgvHienThi.Columns["ngayChungTu"].HeaderText = "Ngày chứng từ";
                dgvHienThi.Columns["ngayChungTu"].Visible = true;
                dgvHienThi.Columns["maHang"].HeaderText = "Mã hàng";
                dgvHienThi.Columns["maHang"].Visible = true;
                dgvHienThi.Columns["tenHang"].HeaderText = "Tên hàng";
                dgvHienThi.Columns["tenHang"].Visible = true;
                dgvHienThi.Columns["soLuong"].HeaderText = "Số lượng";
                dgvHienThi.Columns["soLuong"].Visible = true;
                if (dgvHienThi.RowCount > 0)
                {
                    Common.Utilities.CountDatagridview(dgvHienThi);
                    dgvHienThi.Focus();
                    dgvHienThi.Select();
                }
                dgvHienThi.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                dgvHienThi.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }
        }
        string hanhDong;
        string LayKho(string mak)
        {
            try
            {
                foreach (Entities.KhoHang item in khoh)
                {
                    if (item.MaKho == mak)
                    {
                        return item.TenKho;
                    }
                }
                return "1";
            }
            catch
            {
                return "1";
            }
        }
        private void XuLy(string makho)
        {
            bool kt = false;
            for (int i = 0; i < xuathuy.Phieuxuathuy.Length; i++)
            {
                if (makho == xuathuy.Phieuxuathuy[i].MaKho)
                {
                    kt = true;
                    ChiTietPhieuXuatHuy(xuathuy.Phieuxuathuy[i]);
                }
            }
            if (kt == false)
            {
                danhsach = null;
                MessageBox.Show("trong kho không có dữ liệu");
                return;
            }
        }
        ArrayList list = new ArrayList();
        private void ChiTietPhieuXuatHuy(Entities.PhieuXuatHuy phieuxuathuy)
        {

            for (int i = 0; i < xuathuy.Chitietxuathuy.Length; i++)
            {

                if (phieuxuathuy.MaPhieuXuatHuy == xuathuy.Chitietxuathuy[i].MaPhieuXuatHuy)
                {
                    Entities.BCXuatHuyHangHoa row = new Entities.BCXuatHuyHangHoa();
                    row.MaKho = phieuxuathuy.MaKho;
                    row.MaChungTu = phieuxuathuy.MaPhieuXuatHuy;
                    row.NgayChungTu = phieuxuathuy.NgayLap;
                    row.MaHang = xuathuy.Chitietxuathuy[i].MaHangHoa;
                    row.TenHang = xuathuy.Chitietxuathuy[i].TenHangHoa;
                    row.SoLuong = xuathuy.Chitietxuathuy[i].SoLuong;
                    list.Add(row);
                }
            }
            LayDuLieu();
            dgvHienThi.DataSource = danhsach;
        }
        private void LayDuLieu()
        {
            int n = list.Count;
            if (n == 0) { danhsach = null; }
            danhsach = new Entities.BCXuatHuyHangHoa[n];
            for (int i = 0; i < n; i++)
            {
                danhsach[i] = (Entities.BCXuatHuyHangHoa)list[i];
            }
        }

        #endregion


        #region Thoát
        private void Thoat()
        {
            try
            {
                System.Windows.Forms.DialogResult ketqua = System.Windows.Forms.MessageBox.Show("Bạn có chắc chắn muốn thoát không ?", "Thông báo", System.Windows.Forms.MessageBoxButtons.YesNo);
                {
                    if (ketqua == System.Windows.Forms.DialogResult.Yes)
                    {
                        this.CheckClose = true;
                        this.Close();
                    }
                }
            }
            catch (Exception)
            { }
        }
        #endregion

        #region In
        private void In()
        {

            if (hienthi.Length - 1 == 0)
            {
                return;
            }
            try
            {
                Entities.BCXuatHuyHangHoa[] temp = new Entities.BCXuatHuyHangHoa[hienthi.Length - 1];
                int sotang = 0;
                for (int j = 0; j < hienthi.Length - 1; j++)
                {
                    temp[sotang] = hienthi[j];
                    sotang++;
                }
                this.XuLy(comboBox1.SelectedValue.ToString());
                BaoCao.frmBaoCaorpt frm = new frmBaoCaorpt(temp, makho, batdau, ketthuc);
                if (frm.kiemtra)
                {
                    frm.ShowDialog();

                }
            }
            catch
            {
            }
        }
        #endregion

        #region Lọc điều kiện
        void LocDieuKien()
        {
            try
            {
                list = new ArrayList();
                GUI.LocDieuKien.frmLocDieuKien frm = new LocDieuKien.frmLocDieuKien();
                frm.ShowDialog();
                if (GUI.LocDieuKien.frmLocDieuKien.kiemtra)
                {
                    DateTime truoc1;
                    DateTime sau1;
                    try
                    {
                        truoc1 = DateTime.Parse(GUI.LocDieuKien.frmLocDieuKien.truoc);
                        sau1 = DateTime.Parse(GUI.LocDieuKien.frmLocDieuKien.sau);
                    }
                    catch
                    {
                        return;
                    }
                    batdau = truoc1;
                    ketthuc = sau1;
                    XuLy(comboBox1.SelectedValue.ToString());
                    GUI.LocDieuKien.frmLocDieuKien.kiemtra = false;
                }
            }
            catch
            {
            }
        }
        #endregion

        #region Word
        void Word()
        {
            if (hienthi.Length - 1 == 0)
            {
                return;
            }
            try
            {
                savefile.Filter = "Word |*.doc"; savefile.FileName = "";
                if (savefile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    Entities.BCXuatHuyHangHoa[] temp = new Entities.BCXuatHuyHangHoa[hienthi.Length - 1];
                    int sotang = 0;
                    for (int j = 0; j < hienthi.Length - 1; j++)
                    {
                        temp[sotang] = hienthi[j];
                        sotang++;
                    }
                    BaoCao.frmBaoCaorpt frm = new frmBaoCaorpt(temp, makho, batdau, ketthuc, savefile.FileName, Common.Constants.Word);
                }
            }
            catch
            {
            }
        }
        #endregion

        #region Excel
        void Excel()
        {
            if (hienthi.Length - 1 == 0)
            {
                return;
            }
            try
            {
                savefile.Filter = "Excel |*.xls"; savefile.FileName = "";
                if (savefile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    Entities.BCXuatHuyHangHoa[] temp = new Entities.BCXuatHuyHangHoa[hienthi.Length - 1];
                    int sotang = 0;
                    for (int j = 0; j < hienthi.Length - 1; j++)
                    {
                        temp[sotang] = hienthi[j];
                        sotang++;
                    }
                    BaoCao.frmBaoCaorpt frm = new frmBaoCaorpt(temp, makho, batdau, ketthuc, savefile.FileName, Common.Constants.Excel);
                }
            }
            catch
            {
            }
        }
        #endregion

        #region PDF
        void PDF()
        {
            if (hienthi.Length - 1 == 0)
            {
                return;
            }
            try
            {
                savefile.Filter = "PDF |*.pdf"; savefile.FileName = "";
                if (savefile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    Entities.BCXuatHuyHangHoa[] temp = new Entities.BCXuatHuyHangHoa[hienthi.Length - 1];
                    int sotang = 0;
                    for (int j = 0; j < hienthi.Length - 1; j++)
                    {
                        temp[sotang] = hienthi[j];
                        sotang++;
                    }
                    BaoCao.frmBaoCaorpt frm = new frmBaoCaorpt(temp, makho, batdau, ketthuc, savefile.FileName, Common.Constants.PDF);
                }
            }
            catch
            {
            }
        }
        #endregion
        #region xử lý form
        Entities.KhoHang kh;
        private void frmBCXuatHuyHangHoa_Load(object sender, EventArgs e)
        {
            comboBox1.DropDownClosed += new EventHandler(comboBox1_DropDownClosed);
            dgvHienThi.SelectionChanged +=new EventHandler(dgvHienThi_SelectionChanged);
        }

        private void comboBox1_DropDownClosed(object sender, EventArgs e)
        {
            list = new ArrayList();
            XuLy(comboBox1.SelectedValue.ToString());
        }

        private void dgvHienThi_SelectionChanged(object sender, EventArgs e)
        {
            
            try
            {
                try
                {

                   // i = dgvHienThi.Rows[e.index];
                    danhsach = (Entities.BCXuatHuyHangHoa[])dgvHienThi.DataSource;
                }
                catch { }
                this.phieuxuat = (Entities.PhieuXuatHuy)dgvHienThi.DataSource;
            }
            catch (Exception ex)
            { string s = ex.Message; }
        }
        private void btnin_Click(object sender, EventArgs e)
        {
            In();
        }

        private void btnloc_Click(object sender, EventArgs e)
        {
            LocDieuKien();
        }

        private void btnword_Click(object sender, EventArgs e)
        {
            Word();
        }

        private void btnExcel_Click(object sender, EventArgs e)
        {
            Excel();
        }

        private void btnpdf_Click(object sender, EventArgs e)
        {
            PDF();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Thoat();
        }
        #endregion
    }
}
